package app.project.directionlensproject.map;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import app.project.directionlensproject.HomeActivity;
import app.project.directionlensproject.R;
import app.project.directionlensproject.dataparser.JSONParser;
import app.project.directionlensproject.model.Place;
import app.project.directionlensproject.util.BundleKeys;
import app.project.directionlensproject.util.CommonUtils;
import app.project.directionlensproject.util.Constants;
import app.project.directionlensproject.util.Utilities;

public class PlaceDirectionActivity extends Activity implements OnClickListener {

	private GoogleMap map;
//	public static final LatLng HANOI = new LatLng(20.993776, 105.811417);
//	public static final LatLng HOGUOM = new LatLng(21.028667, 105.852148);
	public LatLng MYPLACE;
	public Place place;
	private TextView mTextViewUser;
	private int categoryID;
	@Override
	public void onCreate(Bundle circle) {
		super.onCreate(circle);
		setContentView(R.layout.activity_place_direction);
		MYPLACE = new LatLng(HomeActivity.MY_LATITUDE,
				HomeActivity.MY_LONGITUDE);
		categoryID=getIntent().getIntExtra(BundleKeys.CATE_ID,0);
		Log.e("CategoryDirection",categoryID+"");
		String nameUser = CommonUtils.getStringSetting(
				PlaceDirectionActivity.this, Constants.USER_NAME, "Guest");
		mTextViewUser = (TextView) findViewById(R.id.textViewUserName);
		mTextViewUser.setText(getResources().getString(R.string.name_hello) + nameUser);

		createButtonView();
		createMapViewInitial();
	}

	private void createButtonView() {
		findViewById(R.id.buttonDriving).setOnClickListener(this);
		findViewById(R.id.buttonCycling).setOnClickListener(this);
		findViewById(R.id.buttonWalking).setOnClickListener(this);
		findViewById(R.id.imageViewZoomMyPlaceDirection).setOnClickListener(this);
	}

	private void createMapViewInitial() {
		MarkerOptions mkOption = new MarkerOptions().position(MYPLACE)
				.title("My location!").snippet("")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_my_place));

		place = (Place) getIntent().getSerializableExtra("place");
		MarkerOptions mkOption1 = new MarkerOptions()
				.position(new LatLng(place.locationx, place.locationy))
				.title(place.placename)
				.snippet(place.description)
				.icon(Utilities.getInstance().getBitmapDescriptionFromType(
						place.categoryid));
		
		if(place.categoryid==0){
			mkOption1.icon(Utilities.getInstance().getBitmapDescriptionFromType(categoryID));
		}
		
		map = ((MapFragment) getFragmentManager().findFragmentById(
				R.id.mapPlaceDirection)).getMap();

		// add maker to map
		map.addMarker(mkOption);
		map.addMarker(mkOption1);
		// Move the camera instantly to HANOI with a zoom of 30.
		map.moveCamera(CameraUpdateFactory.newLatLngZoom(MYPLACE, 30));

		// Zoom in, animating the camera.
		map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

		String urlPlaces = Utilities.getInstance().makeURL(MYPLACE.latitude,
				MYPLACE.longitude, place.locationx, place.locationy,
				Constants.MODE_DRIVING);
		new getPathPlaceTask(urlPlaces).execute();
	}

	/**
	 * Task get place from DB
	 * 
	 * @author tuanshaker
	 * 
	 */
	public class getPathPlaceTask extends AsyncTask<Void, Void, String> {
		private ProgressDialog progressDialog;
		String url;

		getPathPlaceTask(String urlPass) {
			url = urlPass;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progressDialog = new ProgressDialog(PlaceDirectionActivity.this);
			progressDialog.setMessage(getResources().getString(
					R.string.name_loading_map));
			progressDialog.setIndeterminate(true);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {
			JSONParser jParser = new JSONParser();
			String json = jParser.getJSONFromUrl(url);
			return json;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.hide();
			if (result != null) {
				Utilities.getInstance().drawPath(map, result);
			}
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.buttonDriving:
			if (Utilities.getInstance()
					.isAppInstalled("com.google.android.apps.maps",
							PlaceDirectionActivity.this)) {
				Utilities.getInstance().gotoGoogleMap(
						PlaceDirectionActivity.this,
						new LatLng(place.locationx, place.locationy),
						Constants.MODE_DRIVING);
			} else {
				startActivity(new Intent(PlaceDirectionActivity.this,
						PlaceDirectionDetailActivity.class).putExtra(
						"typeDirection", Constants.MODE_DRIVING).putExtra(
						"place", place));
			}
			break;
		case R.id.buttonCycling:
			if (Utilities.getInstance()
					.isAppInstalled("com.google.android.apps.maps",
							PlaceDirectionActivity.this)) {
				Utilities.getInstance().gotoGoogleMap(
						PlaceDirectionActivity.this,
						new LatLng(place.locationx, place.locationy),
						Constants.MODE_CYCLING);
			} else {
				startActivity(new Intent(PlaceDirectionActivity.this,
						PlaceDirectionDetailActivity.class).putExtra(
						"typeDirection", Constants.MODE_CYCLING).putExtra(
						"place", place));
			}
			break;
		case R.id.buttonWalking:
			if (Utilities.getInstance()
					.isAppInstalled("com.google.android.apps.maps",
							PlaceDirectionActivity.this)) {
				Utilities.getInstance().gotoGoogleMap(
						PlaceDirectionActivity.this,
						new LatLng(place.locationx, place.locationy),
						Constants.MODE_WALKING);
			} else {
				startActivity(new Intent(PlaceDirectionActivity.this,
						PlaceDirectionDetailActivity.class).putExtra(
						"typeDirection", Constants.MODE_WALKING).putExtra(
						"place", place));
			}
			break;
		case R.id.imageViewZoomMyPlaceDirection:
			// Move the camera instantly to HANOI with a zoom of 30.
			map.moveCamera(CameraUpdateFactory.newLatLngZoom(MYPLACE, 30));
			// Zoom in, animating the camera.
			map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
		default:
			break;
		}
	}
}
